<?php
/**
 *	[TWCMS] (C)2012 TongWang Inc.
 *	$Id: content_links.php 2 2013-04-27 07:14:17Z nanown@qq.com $
 */
class content_links extends admin{
	public $status;
	public $count;

	function __construct() {
		parent::__construct();
		$this->status = array('首页', '内页', '禁用');
		$this->count = $this->countLinks();
		$this->main();
	}

	function main() {
		if(isset($_GET['ajax'])) {
			if(in_array($_GET['ajax'], array('add_edit', 'del', 'batch_del', 'type_add_edit', 'get_link_id', 'get_links', 'get_links_type', 'get_links_type_select', 'type_del'))) {
				$func = 'ajax_'.$_GET['ajax'];
				$this->$func();
			}
		}else{
			include load_tpl(__CLASS__);
		}
	}

	#添加
	function ajax_add_edit() {
		$_POST = _trim($_POST);
		$linkid = intC($_POST, 'linkid');
		$_POST['type'] = intC($_POST, 'type');
		$_POST['status'] = intC($_POST, 'status');
		$_POST['orderby'] = intC($_POST, 'orderby');
		$err = 1;
		$name = $msg = '';
		if(empty($_POST['name'])) {
			$name = 'name';
			$msg = '网站名称不能为空';
		}elseif(empty($_POST['url'])) {
			$name = 'url';
			$msg = '站点 URL不能为空';
		}elseif(empty($linkid)) {	#添加
			if($_POST['orderby'] == 0) {
				$_POST['orderby'] = $this->db->result_first('SELECT orderby FROM `'.TW_DBPRE.'links` ORDER BY `orderby` DESC LIMIT 1')+1;
			}

			if(!$this->db->query('INSERT INTO `'.TW_DBPRE.'links` (`type`, `status`, `name`, `url`, `logo`, `note`, `time`, `orderby`) VALUES ('.$_POST['type'].', '.$_POST['status'].', '.S($_POST['name']).', '.S($_POST['url']).', '.S($_POST['logo']).', '.S($_POST['note']).', '.time().', '.$_POST['orderby'].')')){
				$msg = '操作数据库出错';			
			}else{
				$err = 0;
				$msg = '添加完成';
			}
		}else{	#编辑
			unset($_POST['linkid']);
			if(!$this->db->update('links', $_POST, '`id`='.$linkid)){
				$msg = '操作数据库出错';			
			}else{
				$err = 0;
				$msg = '编辑完成';
			}
		}
		exit('{"err":'.$err.',"name":"'.$name.'","msg":"'.$msg.'"}');
	}

	#删除
	function ajax_del() {
		$err = 1;
		if(empty($_POST['id'])) {
			$msg = '非法访问';
		}elseif(!$this->db->query('DELETE FROM `'.TW_DBPRE.'links` WHERE `id`='.intval($_POST['id']))) {
			$msg = '操作数据库出错';			
		}else{
			$err = 0;
			$msg = '删除完成';
		}
		exit('{"err":'.$err.',"msg":"'.$msg.'"}');
	}

	#批量删除
	function ajax_batch_del() {
		$err = 1;
		$idStr = '';
		if(isset($_POST['idarr']) && is_array($_POST['idarr'])) {
			foreach($_POST['idarr'] as $val) {
				$val = intval($val);
				if($val) {
					$idStr .= $val.',';
				}
			}
			$idStr = rtrim($idStr, ',');
		}
		if(empty($idStr)) {
			$msg = '非法访问';
		}elseif(!$this->db->query('DELETE FROM `'.TW_DBPRE.'links` WHERE `id` IN('.$idStr.')')) {
			$msg = '操作数据库出错';			
		}else{
			$err = 0;
			$msg = '删除完成';
		}
		exit('{"err":'.$err.',"msg":"'.$msg.'"}');
	}

	#AJAX显示全部
	function ajax_get_links() {
		$this->get_links();
	}

	#根据ID显示某一条
	function ajax_get_link_id() {
		$id = intget('id');
		if(empty($id)) {
			$sql = 'SELECT * FROM `'.TW_DBPRE.'links` L LEFT JOIN `'.TW_DBPRE.'links_type` T ON L.type=T.typeid ORDER BY L.`id` DESC LIMIT 1';
		}else{
			$sql = 'SELECT * FROM `'.TW_DBPRE.'links` L LEFT JOIN `'.TW_DBPRE.'links_type` T ON L.type=T.typeid WHERE L.id='.$id;
		}
		$row = $this->db->fetch_first($sql);
		$this->display($row);
	}

	#显示全部
	function get_links() {
		$query = $this->db->query('SELECT * FROM `'.TW_DBPRE.'links` L LEFT JOIN `'.TW_DBPRE.'links_type` T ON L.type=T.typeid ORDER BY L.`orderby` DESC,L.`id` DESC');
		while($row = $this->db->fetch_array($query)) {
			$this->display($row);
		}
	}

	#显示
	function display($row) {
		echo '<tr class="li">';
		echo '<td><input type="checkbox" val="'.$row['id'].'" title="ID:'.$row['id'].'&#10;添加时间：'.date('Y-m-d H:i:s', $row['time']).'" name="linkid"></td>';
		echo '<td><input type="text" class="inp" name="orderby" value="'.$row['orderby'].'" size="3"></td>';
		echo '<td><input type="text" class="inp" name="name" value="'.$row['name'].'" size="15"></td>';
		echo '<td><input type="text" class="inp" name="url" value="'.$row['url'].'" size="20"></td>';
		echo '<td><input type="text" class="inp" name="logo" value="'.$row['logo'].'" size="20"></td>';
		echo '<td><input type="text" class="inp" name="note" value="'.$row['note'].'" size="30"></td>';
		echo '<td><div class="linkse inp" val="'.$row['status'].'">'.$this->status[$row['status']].'</div></td>';
		echo '<td><div class="linkse inp" val="'.$row['type'].'">'.$row['typename'].'</div></td>';
		echo '<td>';
		echo '<a class="but3" href="javascript:L.del('.$row['id'].');">删除</a>';
		echo '</td>';
		echo '</tr>';
	}

	#共多少条友情链接
	function countLinks() {
		return $this->db->result_first('SELECT count(*) FROM `'.TW_DBPRE.'links`');
	}

	#获取链接分类
	function ajax_get_links_type() {
		$query = $this->db->query('SELECT * FROM `'.TW_DBPRE.'links_type` ORDER BY `typeid` ASC');
		while($row = $this->db->fetch_array($query)) {
			echo '<tr class="li"><td>'.$row['typeid'].'</td><td><input class="inp" name="typename" type="text" typeid="'.$row['typeid'].'" value="'.$row['typename'].'" /></td></tr>';
		}
	}

	#获取链接分类SELECT
	function ajax_get_links_type_select() {
		$query = $this->db->query('SELECT * FROM `'.TW_DBPRE.'links_type` ORDER BY `typeid` ASC');
		while($row = $this->db->fetch_array($query)) {
			echo '<option value="'.$row['typeid'].'">'.$row['typename'].'</option>';
		}
	}

	#链接分类新增/编辑
	function ajax_type_add_edit() {
		$addSql = $editSql = array();
		if(isset($_POST['add']) && is_array($_POST['add'])) {
			foreach($_POST['add'] as $val) {
				$val = trim($val);
				if($val) {
					$addSql[] = 'INSERT INTO `'.TW_DBPRE.'links_type`(`typename`) VALUES ('.S($val).')';
				}
			}
		}
		if(isset($_POST['edit']) && is_array($_POST['edit'])) {
			foreach($_POST['edit'] as $arr) {
				if(!is_array($arr)) break;
				$typeid = intval($arr[0]);
				$typename = trim($arr[1]);
				if($typeid && $typename) {
					$editSql[$typeid] = 'UPDATE `'.TW_DBPRE.'links_type` SET `typename`='.S($typename).' WHERE `typeid`='.$typeid;
				}
			}
		}
		$err = 1;
		$isAdd = $isEdit = true;
		if($addSql) {
			foreach($addSql as $sql) {
				$this->db->query($sql) || $isAdd = false;
			}
		}
		if($editSql) {
			foreach($editSql as $sql) {
				$this->db->query($sql) || $isEdit = false;
			}
		}
		if(!$isAdd || !$isEdit) {
			$msg = '数据库出错';		
		}else{
			$err = 0;
			$msg = '操作完成';
		}
		exit('{"err":'.$err.',"msg":"'.$msg.'"}');
	}

	#链接分类删除
	function ajax_type_del() {
		$err = 1;
		$typeid = intC($_POST, 'typeid');
		if(empty($typeid)) {
			$msg = '非法访问';
		}elseif($typeid == 1) {
			$msg = '不允许删除(ID:1)的链接分类';
		}elseif($countNum=$this->db->result_first('SELECT count(*) FROM `'.TW_DBPRE.'links` WHERE `type`='.$typeid)) {
			$msg = '链接分类存在 '.$countNum.' 条链接，不允许删除';
		}elseif(!$this->db->query('DELETE FROM `'.TW_DBPRE.'links_type` WHERE `typeid`='.$typeid)) {
			$msg = '操作数据库出错';			
		}else{
			$err = 0;
			$msg = '删除完成';
		}
		exit('{"err":'.$err.',"msg":"'.$msg.'"}');
	}
}